﻿using System.Collections.Generic;
using Google.YouTube;

namespace YouTubeDownloadExtension
{
    public static class YouTubeApiExtensions
    {
        public static void Download(this YouTubeRequest request, Video video, VideoQuality preferredQuality = VideoQuality.HD1080, VideoFormat preferredFormat = VideoFormat.MP4, string downloadFolder = "")
        {
            var source = video.GetSources().SpecificOrBest(preferredQuality, preferredFormat);
            var fileName = string.Format("{0}.{1}", video.Title, source.Format.ToFileExtension());
            var download = new Downloader(source.Uri, fileName) { DownloadFolder = downloadFolder };
            download.DownloadFile();
        }
                
        public static IEnumerable<VideoSource> GetSources(this Video video)
        {
            var builder = new VideoSourceBuilder(video.VideoId);
            builder.BuildSources();
            return builder.GetSources();
        }
    }
}